package cc.mrbird.handle.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.handle.domain.Handle;
import cc.mrbird.handle.service.HandleService;
import cc.mrbird.submission.util.RestUtil;
import cc.mrbird.submission.util.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class HandleController extends BaseController {
    @Autowired
    private HandleService handleService;

    @RequestMapping("handle")
    public String handle(Model model) {

        return "handle/handle";
    }

    @Log("获取投诉信息列表")
    @RequestMapping("handle/handleList")
    @ResponseBody
    public Map<String, Object> handleList(QueryRequest request, Handle handle) {
        System.out.println("handle:"+handle.getId());

        return super.selectByPageNumSize(request, () -> this.handleService.getHandleList(handle));
    }

    @Log("根据id获取投诉信息")
    @RequestMapping("handle/getByHandleId")
    @ResponseBody
    public ResponseBo getBySubmissionId(Handle handle) {
        String id = handle.getId();
        handle.setId(id);
        return ResponseBo.ok().put("data",handleService.getByHandleId(handle));
    }

    @Log("修改投诉信息")
    @RequestMapping("handle/updateHandle")
    @ResponseBody
    public ResponseBo updateHandle(Handle handle) throws Exception {
        if(handle.getProcessing().equals("已处理")||handle.getProcessing()=="已处理"){
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            handle.setProcessingTime(dateString);
            WXPayUtil wxPayUtil = new WXPayUtil();
            Map map = new HashMap();
            //通知三亚后台人员
            String idNotice = "111";
            String content = null;
            content = "你好，技术员已经处理好定位/故障，投诉编号为："+handle.getId()+"，请注意查看";
            String keyid = "2";
            String random_number = wxPayUtil.generateNonceStr();
            String time_stamp = String.valueOf(System.currentTimeMillis());
            String key = "EL6UjSc4U1it6vaFrQ9GlGiMicEbRc0P";
            //通知编号
            map.put("id",idNotice);
            //通知内容
            map.put("content",content);
            //keyid
            map.put("keyid",keyid);
            //随机数
            map.put("random_number",random_number);
            //时间戳
            map.put("time_stamp",time_stamp);
            String sign = wxPayUtil.generateSignature(map,key);
            String url = "http://120.77.182.149:8999/OpenApi/tyxNotice";
            System.out.println(url);
            String data = "id="+idNotice+"&content="+content+"&keyid="+keyid+"&random_number="+random_number+"&time_stamp="+time_stamp+"&sign="+sign;
            System.out.println("data:"+data);
            RestUtil restUtil = new RestUtil();
            restUtil.interfaceUtil(url,data);
        }
        handleService.updateHandle(handle);
        return ResponseBo.ok();
    }

}
